package com.haochang.chunkvod.app.utils;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseItemClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.DialogInterface.OnShowListener;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * 通用提示框
 * @author qintao
 *
 */
@SuppressLint("CutPasteId")
public class VodDialog
{
	private Builder builder;

	protected VodDialog(Builder builder)
	{
		this.builder = builder;
	}

	@SuppressLint("ClickableViewAccessibility")
	public void show()
	{
		if (builder.context == null)
			return;

		if (!(builder.context instanceof Activity) || ((Activity) builder.context).isFinishing())
			return;

		//关闭正在显示的dialog
		if (customDialog != null && customDialog.isShowing())
		{
			if (isExclusiveMode)//独占模式的dialog处于显示状态，不受理其他dialog的显示
				return;
			try
			{
				customDialog.dismiss();
			} catch (Exception e)
			{
			}
			customDialog = null;
		}

		//内容不能为空
		if ((builder.dialogEnum == DialogEnum.SINGLE || builder.dialogEnum == DialogEnum.DOUBLE || builder.dialogEnum == DialogEnum.ONLY_TOAST
				|| builder.dialogEnum == DialogEnum.SINGLE_CUSTOM_VIEW) && TextUtils.isEmpty(builder.contentName))
			return;

		//创建dialog
		customDialog = new AlertDialog.Builder(builder.context, DialogEnum.getStyleForType(builder.dialogEnum)).create();
		if (builder.dialogEnum != DialogEnum.LIST)
		{
			customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
		customDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		customDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
		if (builder.dialogEnum == DialogEnum.SINGLE || builder.dialogEnum == DialogEnum.DOUBLE)
		{
			customDialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
		} else if (builder.dialogEnum == DialogEnum.LIST)
		{
			customDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
			customDialog.getWindow().setWindowAnimations(R.style.dialogListWindowAnim);
		}

		customDialog.setCanceledOnTouchOutside(builder.isDismissOnTouchOutside);
		customDialog.setCancelable(builder.cancelable);
		customDialog.show();

		int contentViewResId = DialogEnum.getLayoutForType(builder.dialogEnum);
		if (contentViewResId < 0)
			return;
		customDialog.getWindow().setContentView(contentViewResId);
		View view = customDialog.getWindow().getDecorView();
		view.setTag(builder);//用 TAG 保存,当前 Dialog 的 Builder 对象信息.
		if (builder.onShowListener != null)
			customDialog.setOnShowListener(builder.onShowListener);

		if (builder.onCancelListener != null)
			customDialog.setOnCancelListener(builder.onCancelListener);

		customDialog.setOnDismissListener(new OnDismissListener()
		{
			OnDismissListener onDismissListener = builder.onDismissListener;

			@Override
			public void onDismiss(DialogInterface dialog)
			{
				//当dialog被销毁时，取消独占模式
				if (customDialog == null || customDialog == dialog)
					isExclusiveMode = false;

				if (onDismissListener != null)
				{
					builder.onDismissListener.onDismiss(dialog);
				}
			}
		});
		isExclusiveMode = builder.isExclusiveMode;
		if (builder.onKeyListener != null)
			customDialog.setOnKeyListener(builder.onKeyListener);

		switch (builder.dialogEnum)
		{
		case ONLY_TOAST:
			BaseTextView tv_content_info = (BaseTextView) view.findViewById(R.id.tv_content_info);
			ImageView img_close = (ImageView) view.findViewById(R.id.img_close);
			if (!TextUtils.isEmpty(this.builder.contentName) && null != tv_content_info)
			{
				tv_content_info.setText(this.builder.contentName);
				tv_content_info.setGravity(this.builder.gravity);
				tv_content_info.setMovementMethod(ScrollingMovementMethod.getInstance());
			}
			img_close.setOnClickListener(onBtClickListener);
			// XXX 不同系统对setCancelable实现不同 设置为false 索尼屏蔽返回键功能  未发现问题 需测试
			customDialog.setOnCancelListener(new OnCancelListener()
			{
				@Override
				public void onCancel(DialogInterface arg0)
				{
					if (null != builder.customDialogListener)
						builder.customDialogListener.onCancelClick();
					if (null == customDialog || customDialog != arg0)
						return;
					//在回调之前取消独占模式，避免在回调中打开新Dialog被独占模式屏蔽
					isExclusiveMode = false;
					customDialog.dismiss();
					customDialog = null;
				}
			});
			break;
		case SINGLE:
		case SINGLE_CUSTOM_VIEW:
		case DOUBLE_CUSTOM_VIEW:
		case DOUBLE:
		{
			RelativeLayout checkOut_div = (RelativeLayout) view.findViewById(R.id.checkOut_div);
			BaseTextView tv_content = (BaseTextView) view.findViewById(R.id.tv_content);
			Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
			btn_sure = (Button) view.findViewById(R.id.btn_sure);
			Button btn_only_cancel = (Button) view.findViewById(R.id.btn_only_cancel);
			LinearLayout btn_layout = (LinearLayout) view.findViewById(R.id.btn_layout);
			CheckBox item_cb = (CheckBox) view.findViewById(R.id.item_cb);
			item_cb.setChecked(true);
			item_cb.setOnCheckedChangeListener(mOnCheckedChangeListener);
			if (builder.dialogEnum == DialogEnum.SINGLE || builder.dialogEnum == DialogEnum.SINGLE_CUSTOM_VIEW)
			{
				btn_only_cancel.setVisibility(View.VISIBLE);
				btn_layout.setVisibility(View.GONE);
			} else
			{
				btn_layout.setVisibility(View.VISIBLE);
				btn_only_cancel.setVisibility(View.GONE);
			}
			if (builder.dialogEnum == DialogEnum.DOUBLE_CUSTOM_VIEW)
			{
				checkOut_div.setVisibility(View.VISIBLE);
			}
			//点击按钮设值
			btn_sure.setText(builder.btnNeutralText);
			btn_cancel.setText(builder.btnNegativeText);
			btn_cancel.setOnClickListener(onBtClickListener);
			btn_sure.setOnClickListener(onBtClickListener);
			btn_only_cancel.setText(builder.btnPositiveText);
			btn_only_cancel.setOnClickListener(onBtClickListener);
			if (!TextUtils.isEmpty(this.builder.contentName) && null != tv_content)
			{
				if (builder.dialogEnum == DialogEnum.SINGLE_CUSTOM_VIEW)
				{
					Logger.e(this.builder.contentName + "");
					tv_content.setText(Html.fromHtml((String) this.builder.contentName));
				} else
				{
					tv_content.setText(this.builder.contentName);
				}
				tv_content.setMovementMethod(ScrollingMovementMethod.getInstance());
			}
			// XXX 不同系统对setCancelable实现不同 设置为false 索尼屏蔽返回键功能  未发现问题 需测试
			customDialog.setOnCancelListener(new OnCancelListener()
			{
				@Override
				public void onCancel(DialogInterface arg0)
				{
					if (null != builder.customDialogListener)
						builder.customDialogListener.onCancelClick();
					if (null == customDialog || customDialog != arg0)
						return;
					//在回调之前取消独占模式，避免在回调中打开新Dialog被独占模式屏蔽
					isExclusiveMode = false;
					customDialog.dismiss();
					customDialog = null;
				}
			});
		}
			break;
		case LIST:
		{
			customDialog.getWindow().setGravity(Gravity.CENTER);
			customDialog.getWindow().setLayout(android.view.WindowManager.LayoutParams.MATCH_PARENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
			BaseListView state_list = (BaseListView) view.findViewById(R.id.state_list);
			if (!TextUtils.isEmpty(builder.listTitle))
			{
				View header = LayoutInflater.from(builder.context).inflate(R.layout.common_dialog_toast_list_header_view, null);
				BaseTextView tv_title = (BaseTextView) header.findViewById(R.id.tv_title);
				tv_title.setText(builder.listTitle + "");
				tv_title.setTextColor(builder.context.getResources().getColor(R.color.color_f04e39));
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
						DipPxConversion.dipToPx(builder.context, 50));
				tv_title.setLayoutParams(params);
				state_list.addHeaderView(header);
			}
			BaseAdapter adapter = new ArrayAdapter<String>(builder.context, R.layout.common_dialog_toast_list_item, builder.dialogList);
			state_list.setAdapter(adapter);
			state_list.setSelector(R.color.white);
			state_list.setOnItemClickListener(new OnBaseItemClickListener()
			{

				@Override
				public void onBaseItemClick(AdapterView<?> parent, View view, int position, long id)
				{
					if (null == customDialog || position >= parent.getAdapter().getCount())
						return;
					if (builder.isAutoDismiss && customDialog.isShowing())
					{
						//在回调之前取消独占模式，避免在回调中打开新Dialog被独占模式屏蔽
						isExclusiveMode = false;

						customDialog.dismiss();
						customDialog = null;
					}
					if (null != builder.dialogListListener)
					{
						builder.dialogListListener.onSelectItem(position);
					}
				}
			});
			// XXX 不同系统对setCancelable实现不同 设置为false 索尼屏蔽返回键功能  未发现问题 需测试
			customDialog.setOnCancelListener(new OnCancelListener()
			{
				@Override
				public void onCancel(DialogInterface arg0)
				{
					//在回调之前取消独占模式，避免在回调中打开新Dialog被独占模式屏蔽
					isExclusiveMode = false;
					if (null == customDialog)
						return;
					if (null != builder.customDialogListener)
						builder.customDialogListener.onCancelClick();
					customDialog.dismiss();
					customDialog = null;
				}
			});
		}
			break;

		case LOADING:
		{
			final ImageView animView = (ImageView) customDialog.getWindow().getDecorView().findViewById(R.id.loadIV);
			if (animView == null || builder.context == null)
				return;
			LinearInterpolator lirloading = new LinearInterpolator();
			animView.setImageResource(R.drawable.public_loading1);
			RotateAnimation animation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
			animation.setDuration(1200);//设置动画持续时间
			animation.setFillAfter(true);//保持执行后的效果
			animation.setRepeatCount(RotateAnimation.INFINITE);//无限循环
			animation.setInterpolator(lirloading);//加速器
			animView.startAnimation(animation);
		}
			break;

		case LOADING_PAY:
		{
			final ImageView iv_circle = (ImageView) customDialog.getWindow().getDecorView().findViewById(R.id.loadIV);
			if (iv_circle == null || builder.context == null)
				return;
			LinearInterpolator inp = new LinearInterpolator();
			iv_circle.setImageResource(R.drawable.pay_loading);
			Animation an = AnimationUtils.loadAnimation(builder.context, R.anim.reply_foot_rotate);
			an.setFillAfter(true);//保持执行后的效果
			an.setInterpolator(inp);
			iv_circle.startAnimation(an);
		}
			break;

		default:
			break;
		}
	}

	/**
	 * 显示window全宽，marginLeft，marginRight 20dp
	 * @param marginLeftAndmarginRight 左边距和右边距的值dp
	 * 如果小于0或者大于200则置为20
	 *   ONLY_TOAST 布局   android:layout_marginLeft="30dp"
	                       android:layout_marginRight="30dp"
	 */
	public void showWithWindowParams(int marginLeftAndmarginRight)
	{
		show();
		Window window = customDialog.getWindow();
		if (window != null)
		{
			WindowManager.LayoutParams layoutParams = window.getAttributes();
			layoutParams.width = ScreenUtils.getScreenWidth(builder.context) - 2 * DipPxConversion.dip2px(builder.context, marginLeftAndmarginRight>=200||marginLeftAndmarginRight<0?0:marginLeftAndmarginRight);
			window.setAttributes(layoutParams);
			window.setGravity(Gravity.CENTER);
			window.setBackgroundDrawable(new ColorDrawable(0));
			customDialog.setCancelable(true);
			customDialog.setCanceledOnTouchOutside(true);
		}
	}

	private Button btn_sure;
	private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		{
			switch (buttonView.getId())
			{
			case R.id.item_cb:
				updateBtnSureStatus(isChecked);
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 改变btn的状态
	 * @param isChecked
	 */
	private void updateBtnSureStatus(boolean isChecked)
	{
		btn_sure.setClickable(isChecked);
		btn_sure.setEnabled(isChecked);
	}

	public OnClickListener onBtClickListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			if (null == customDialog)
				return;
			if (builder.isAutoDismiss && customDialog.isShowing())
			{
				//在回调之前取消独占模式，避免在回调中打开新Dialog被独占模式屏蔽
				isExclusiveMode = false;

				customDialog.dismiss();
				customDialog = null;
			}
			if (null != builder.customDialogListener)
			{
				switch (v.getId())
				{
				case R.id.btn_sure:
					builder.customDialogListener.onOkClick();
					break;
				case R.id.btn_cancel:
					builder.customDialogListener.onCancelClick();
					break;
				case R.id.btn_only_cancel:
					builder.customDialogListener.onOnlyCancelClick();
				case R.id.img_close:
					builder.customDialogListener.onOnlyCancelClick();
					break;
				}
			}
		}
	};

	/**
	 * 打开等待提示框
	 */
	private static AlertDialog customDialog = null;

	/**
	 * 记录当前显示的dialog是否为独占模式，如果为true，当前dialog处于显示状态，其他dialog的显示请求将不被执行
	 */
	private static boolean isExclusiveMode;

	/**
	 * 关闭等待提示框
	 */
	public static synchronized void closeDialog()
	{
		try
		{
			if (customDialog != null && customDialog.isShowing())
			{
				customDialog.setOnCancelListener(null);
				customDialog.cancel();
				customDialog = null;
			}
		} catch (Exception e)
		{
			e.printStackTrace();
			customDialog = null;
		}
	}

	/**
	 * 关闭指定类型的对话框.其他类型对话框 保持状态
	 * @param dialogType
	 */
	public static synchronized void closeDialog(DialogEnum dialogType)
	{
		if (dialogType != null && customDialog != null && customDialog.isShowing())
		{
			View view = customDialog.getWindow().getDecorView();
			if (view != null)
			{
				Object tag = view.getTag();
				if (tag != null && tag instanceof Builder)
				{
					Builder builder = (Builder) tag;
					if (builder.dialogEnum == dialogType)
						closeDialog();
				}
			}
		}
	}

	/**
	 * 是否有customDialog显示
	 * @return
	 */
	public static synchronized boolean dialogIsShowing()
	{
		boolean isShowing = false;
		if (customDialog != null && customDialog.isShowing())
		{
			isShowing = true;
		}
		return isShowing;
	}

	/**
	 * 是否有指定类型的customDialog显示
	 * @return
	 */
	public static synchronized boolean dialogIsShowing(DialogEnum dialogType)
	{
		boolean isShowing = false;
		if (dialogType != null && customDialog != null && customDialog.isShowing())
		{
			View view = customDialog.getWindow().getDecorView();
			if (view != null)
			{
				Object tag = view.getTag();
				if (tag != null && tag instanceof Builder)
				{
					Builder builder = (Builder) tag;
					if (builder.dialogEnum == dialogType)
						isShowing = true;
				}
			}

		}
		return isShowing;
	}

	public static class Builder
	{
		protected final Context context;
		protected DialogEnum dialogEnum = DialogEnum.DOUBLE;
		//标题 (名称 字体颜色 大小 位置 icon)
		protected CharSequence titleName = "";
		protected CharSequence leftName = "";
		protected CharSequence listTitle = "";
		protected int titleColor = -1;
		protected DialogGravityEnum titleGravityEnum = DialogGravityEnum.GRAVITY_CENTER;
		//内容(字体颜色 大小  行间距)
		protected CharSequence contentName = "";
		protected int gravity = Gravity.CENTER;//内容区域 contentName的位置
		protected int contentColor = -1;
		protected DialogGravityEnum contentGravityEnum = DialogGravityEnum.GRAVITY_CENTER;
		protected float contentLineSpacingMultiplier = 1.3f;//行间距
		/***
		 * 多按钮自定义View
		 */
		protected View multi_custom_view;
		/**
		 * 按钮
		 */
		//确定按钮
		protected CharSequence btnPositiveText = "";
		protected int btnPositiveColor = -1;
		protected DialogGravityEnum btnPositiveGravityEnum = DialogGravityEnum.GRAVITY_RIGHT;
		//普通按钮
		protected CharSequence btnNeutralText = "";
		protected int btnNeutralColor = -1;
		protected DialogGravityEnum btnNeutralGravityEnum = DialogGravityEnum.GRAVITY_CENTER;
		//取消按钮
		protected CharSequence btnNegativeText = "";
		protected int btnNegativeColor = -1;
		protected DialogGravityEnum btnNegativeGravityEnum = DialogGravityEnum.GRAVITY_LEFT;

		private boolean isAutoDismiss = true;
		private boolean isDismissOnTouchOutside = true;

		//独占模式，当设置为true，在本dialog被关闭之前，其他dialog的Show方法都会被拦截，不被显示
		private boolean isExclusiveMode = false;
		//监听
		protected OnDismissListener onDismissListener = null;
		protected OnCancelListener onCancelListener = null;
		protected OnShowListener onShowListener = null;
		protected OnKeyListener onKeyListener = null;
		//设置能否点击返回
		protected boolean cancelable = true;
		protected List<String> dialogList = new ArrayList<String>();

		public Builder(Context context)
		{
			this.context = context;
			//默认值
			btnPositiveText = this.context.getResources().getString(R.string.dialog_default_understand);
			btnNeutralText = this.context.getResources().getString(R.string.dialog_default_confirm);
			btnNegativeText = this.context.getResources().getString(R.string.dialog_default_cancel);
		}

		public Builder dialogEnum(DialogEnum dialogEnum)
		{
			this.dialogEnum = dialogEnum;
			return this;
		}

		public Builder ListTitleName(CharSequence listTitle)
		{
			if (!TextUtils.isEmpty(listTitle))
				this.listTitle = listTitle;
			return this;
		}

		public Builder ListTitleName(int resId)
		{
			if (context != null && resId > 0)
				ListTitleName(this.context.getString(resId));
			return this;
		}

		/**
		 * 标题
		 */
		public Builder titleName(int resId)
		{
			if (context != null && resId > 0)
				titleName(this.context.getString(resId));
			return this;
		}

		/**
		 * 左标题
		 */
		public Builder leftTitleName(int resId)
		{
			if (context != null && resId > 0)
				titleName(this.context.getString(resId));
			return this;
		}

		/**
		 * 独占模式，当本dialog处于显示当中，其他dialog的show方法会被拦截，不被显示
		 * @return
		 */
		public Builder exclusiveMode()
		{
			isExclusiveMode = true;
			return this;
		}

		/**
		 * 设置自定义View
		 * @Description 
		 * @author Wang Zu Song Patrick
		 * @date 2016-5-16 上午11:56:21 
		 * @param view
		 * @return
		 */
		public Builder multiCustomView(View view)
		{
			if (null != view)
				this.multi_custom_view = view;
			return this;
		}

		public Builder titleName(CharSequence titleName)
		{
			if (!TextUtils.isEmpty(titleName))
				this.titleName = titleName;
			return this;
		}

		public Builder leftTitleName(CharSequence leftName)
		{
			if (!TextUtils.isEmpty(leftName))
				this.leftName = leftName;
			return this;
		}

		public Builder titleColor(int titleColor)
		{
			this.titleColor = titleColor;
			return this;
		}

		public Builder titleColorByRes(int resId)
		{
			if (context != null && resId > 0)
				titleColor(this.context.getResources().getColor(resId));
			return this;
		}

		public Builder titleGravityEnum(DialogGravityEnum titleGravityEnum)
		{
			this.titleGravityEnum = titleGravityEnum;
			return this;
		}

		/**
		 * 内容
		 */
		public Builder contentName(int resId)
		{
			if (this.context != null && resId > 0)
				contentName(this.context.getString(resId));
			return this;
		}

		public Builder contentName(CharSequence contentName)
		{
			if (!TextUtils.isEmpty(contentName))
				this.contentName = contentName;
			return this;
		}

		public Builder contentColor(int contentColor)
		{
			this.contentColor = contentColor;
			return this;
		}

		public Builder contentColorByRes(int resId)
		{
			if (this.context != null && resId > 0)
				contentColor(this.context.getResources().getColor(resId));
			return this;
		}

		public Builder contentGravityEnum(DialogGravityEnum contentGravityEnum)
		{
			this.contentGravityEnum = contentGravityEnum;
			return this;
		}

		public Builder contentLineSpacingMultiplier(float contentLineSpacingMultiplier)
		{
			this.contentLineSpacingMultiplier = contentLineSpacingMultiplier;
			return this;
		}

		/**
		 * 设置内容的位置
		 * @param gravity Gravity.CENTER
		 * @return
		 */
		public Builder setContentGravity(int gravity)
		{
			this.gravity = gravity;
			return this;
		}

		/**
		 * 按钮
		 */
		public Builder btnPositiveText(int resId)
		{
			if (this.context != null && resId > 0)
				btnPositiveText(this.context.getString(resId));
			return this;
		}

		public Builder btnPositiveText(CharSequence btnPositiveText)
		{
			if (!TextUtils.isEmpty(btnPositiveText))
				this.btnPositiveText = btnPositiveText;
			return this;
		}

		public Builder btnPositiveColor(int btnPositiveColor)
		{
			this.btnPositiveColor = btnPositiveColor;
			return this;
		}

		public Builder btnPositiveColorByRes(int resId)
		{
			if (this.context != null && resId > 0)
				btnPositiveColor(this.context.getResources().getColor(resId));
			return this;
		}

		public Builder btnPositiveGravityEnum(DialogGravityEnum btnPositiveGravityEnum)
		{
			this.btnPositiveGravityEnum = btnPositiveGravityEnum;
			return this;
		}

		//普通按钮
		public Builder btnNeutralText(int resId)
		{
			if (this.context != null && resId > 0)
				btnNeutralText(this.context.getString(resId));
			return this;
		}

		public Builder btnNeutralText(CharSequence btnNeutralText)
		{
			if (!TextUtils.isEmpty(btnNeutralText))
				this.btnNeutralText = btnNeutralText;
			return this;
		}

		public Builder btnNeutralColor(int btnNeutralColor)
		{
			this.btnNeutralColor = btnNeutralColor;
			return this;
		}

		public Builder btnNeutralColorByRes(int resId)
		{
			if (this.context != null && resId > 0)
				btnNeutralColor(this.context.getResources().getColor(resId));
			return this;
		}

		public Builder btnNeutralGravityEnum(DialogGravityEnum btnNeutralGravityEnum)
		{
			this.btnNeutralGravityEnum = btnNeutralGravityEnum;
			return this;
		}

		//取消按钮
		public Builder btnNegativeText(int resId)
		{
			if (this.context != null && resId > 0)
				btnNegativeText(this.context.getString(resId));
			return this;
		}

		public Builder btnNegativeText(CharSequence btnNegativeText)
		{
			if (!TextUtils.isEmpty(btnNegativeText))
				this.btnNegativeText = btnNegativeText;
			return this;
		}

		public Builder btnNegativeColor(int btnNegativeColor)
		{
			this.btnNegativeColor = btnNegativeColor;
			return this;
		}

		public Builder btnNegativeColorByRes(int resId)
		{
			if (this.context != null && resId > 0)
				btnNegativeColor(this.context.getResources().getColor(resId));
			return this;
		}

		public Builder btnNegativeGravityEnum(DialogGravityEnum btnNegativeGravityEnum)
		{
			this.btnNegativeGravityEnum = btnNegativeGravityEnum;
			return this;
		}

		/**
		 * 监听
		 */
		public Builder onShowListener(OnShowListener onShowListener)
		{
			this.onShowListener = onShowListener;
			return this;
		}

		public Builder onDismissListener(OnDismissListener onDismissListener)
		{
			this.onDismissListener = onDismissListener;
			return this;
		}

		public Builder onCancelListener(OnCancelListener onCancelListener)
		{
			this.onCancelListener = onCancelListener;
			return this;
		}

		public Builder onKeyListener(OnKeyListener onKeyListener)
		{
			this.onKeyListener = onKeyListener;
			return this;
		}

		//设置能否点击返回 
		public Builder cancelable(boolean cancelable)
		{
			this.cancelable = cancelable;
			return this;
		}

		//不关闭dialog
		public Builder autoDismiss(boolean isAutoDismiss)
		{
			this.isAutoDismiss = isAutoDismiss;
			return this;
		}

		//不关闭dialog
		public Builder cancelableOnTouchOutside(boolean isDismissOnTouchOutside)
		{
			this.isDismissOnTouchOutside = isDismissOnTouchOutside;
			return this;
		}

		// 列表式选项框列表内容
		public Builder dialogList(List<String> dialogList)
		{
			if (this.dialogList == null)
			{
				this.dialogList = new ArrayList<String>();
			}
			this.dialogList.clear();
			this.dialogList.addAll(dialogList);
			return this;
		}

		/**
		 * 显示对话框
		 */
		public VodDialog build()
		{
			VodDialog vodDialog = new VodDialog(this);
			return vodDialog;
		}

		/**
		 * 按钮监听
		 */
		protected OnCustomDialogListener customDialogListener = null;

		public Builder onCustomDialogListener(OnCustomDialogListener listener)
		{
			customDialogListener = listener;
			return this;
		}

		/**
		 * 自定义双按钮对话框事件监听<br/>内部已经处理过 Dialog dismiss .故外部一般调用.无需再处理
		 */
		public interface OnCustomDialogListener
		{
			/**
			 * 确定按钮点击触发
			 */
			void onOkClick();

			/**
			 * 取消按钮点击触发
			 */
			void onCancelClick();

			/**
			 * 单按钮
			 */
			void onOnlyCancelClick();
		}

		/**
		 * 列表对话框监听
		 */
		protected OnDialogListListener dialogListListener = null;

		public Builder onDialogListListener(OnDialogListListener listener)
		{
			dialogListListener = listener;
			return this;
		}

		/**
		 * onSelectItem参数position：选择项的下标
		 * @author jiangshun
		 *
		 */
		public interface OnDialogListListener
		{
			void onSelectItem(int position);
		}
	}

	public enum DialogEnum
	{
		/**
		 * 仅仅弹出框显示信息
		 */
		ONLY_TOAST,
		/**
		 * 单按钮
		 */
		SINGLE,
		/**
		 * 单按钮自定义view样式
		 */
		SINGLE_CUSTOM_VIEW,
		/**
		 * 双按钮
		 */
		DOUBLE,
		/**
		 * 双按钮自定义中间的View样式
		 */
		DOUBLE_CUSTOM_VIEW,
		/**
		 * 列表
		 */
		LIST,
		/**
		 * 默认菊花
		 */
		LOADING,
		/**
		 * 支付菊花
		 */
		LOADING_PAY;

		private static int getLayoutForType(DialogEnum type)
		{
			int returnValue = -1;
			switch (type)
			{
			case ONLY_TOAST:
				returnValue = R.layout.common_dialog_only_toast;
				break;
			case SINGLE:
				returnValue = R.layout.common_dialog_toast;
				break;
			case SINGLE_CUSTOM_VIEW:
				returnValue = R.layout.common_dialog_toast;
				break;
			case DOUBLE:
				returnValue = R.layout.common_dialog_toast;
				break;
			case DOUBLE_CUSTOM_VIEW:
				returnValue = R.layout.common_dialog_toast;
				break;
			case LIST:
				returnValue = R.layout.common_dialog_toast_list;
				break;
			case LOADING:
				returnValue = R.layout.common_load_anim;
				break;
			case LOADING_PAY:
				returnValue = R.layout.common_load_pay_anim;
				break;

			default:
				returnValue = -1;
			}
			return returnValue;
		}

		public static int getStyleForType(DialogEnum type)
		{
			switch (type)
			{
			case ONLY_TOAST:
			case SINGLE:
			case SINGLE_CUSTOM_VIEW:
			case DOUBLE:
			case DOUBLE_CUSTOM_VIEW:
			case LIST:
				return 0;
			case LOADING:
				return R.style.dialog_transparent;
			case LOADING_PAY:
				return 0;
			default:
				return 0;
			}
		}
	}

	public enum DialogGravityEnum
	{
		GRAVITY_LEFT, GRAVITY_CENTER, GRAVITY_RIGHT;
	}
}